<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>27. Variables</title>
</head>
<body>
<h2 class="title">27. Variables</h2>
<p>Remember when we introduced synonyms? This was in the fifth lesson, when
we said that you could make Reeborg learn to recognize other languages. The
example we used was the French word for "move".</p>
<pre>
avance = move
</pre>
<p>Synonyms can be used for numbers too.</p>
<pre>
&gt;&gt;&gt; n = 3
&gt;&gt;&gt; <span class="keyword">print</span> n
3
&gt;&gt;&gt; 2*n
6
&gt;&gt;&gt; n*n
9
</pre>
<p>Nice, isn't it? Now, we can change the value of a synonym as we need
to.</p>
<pre>
&gt;&gt;&gt; n = 3
&gt;&gt;&gt; <span class="keyword">print</span> n
3
&gt;&gt;&gt; n = 2
&gt;&gt;&gt; <span class="keyword">print</span> n
2
</pre>
<p>Because the value of the synonym can change, we call it a <i>variable</i>.
When you are creating a synonym, we say that you are <i>assigning</i> a value
to a variable. The symbol used "<tt>=</tt>" should be read as "is a synonym
for" instead of "is equal to" as you might be used to. This is because, if "n
is equal to 2", then surely "2 is equal to n". Let's try to see what Python
makes of this.</p>
<pre>
&gt;&gt;&gt; n = 2    <span class="comment"># this is ok</span>
&gt;&gt;&gt; 2 = n
SyntaxError: can't assign to literal
</pre>
<p>If a different symbol than the equal sign would have been available on
standard keyboards, like</p>
<pre>
&gt;&gt;&gt; n <font size="6">&larr;</font> 3
&gt;&gt;&gt; <span class="keyword">print</span> n
3
</pre>
<p>it is very likely that Guido van Rossum, the inventor of Python, would
have used it instead to mean "is a synonym for". However, it was not and we
must use the equal sign instead. This also explains why we must use a double
equal sign "<tt>==</tt>" when we want to test if two numbers are equal.</p>
<p>As we have seen briefly above, once a number is assigned to a variable,
that variable can be used in the same way that we would use a number.  
For example:</p>
<pre>
&gt;&gt;&gt; a = 1
&gt;&gt;&gt; b = 2
&gt;&gt;&gt; c = a + b
&gt;&gt;&gt; <span class="keyword">print</span> c
3
</pre>
<p>What we have just seen should be fairly easy to understand.  The following might be a little bit more difficult at first glance:</p>
<pre>
&gt;&gt;&gt; a = 1
&gt;&gt;&gt; a = a + a   <span class="comment"># Surprise?</span>
&gt;&gt;&gt; <span class="keyword">print</span> a
2
</pre>
<p>You must remember that "<tt>=</tt>" should be read as "is a synonym for".  So, the "surprising" line should be understood as follows:</p>
<il>
<li>Take what is on the right hand side of the "=" sign [in this case, <tt>a + a</tt>.]</li>
<li>Calculate its value [2; comes from adding 1 + 1].</li>
<li>Whatever the answer is, the variable on the left hand side [<tt>a</tt> in this case] is a synonym for the answer.</li>
</ul>
<p>Writing it all out like I did make it sound very complicated.  In fact, you will quickly get used to it and very soon it will not surprise you at all.</p>
<p>Because this method for changing the value of a variable occurs so often, Python uses a "condensed" notation:</p>
<pre>
&gt;&gt;&gt; a = 1
&gt;&gt;&gt; a += 3       <span class="comment"># same as</span> <span class="string">"a = a + 3"</span>
&gt;&gt;&gt; <span class="keyword">print</span> a
4

&gt;&gt;&gt; a -= 2       <span class="comment"># same as</span> <span class="string">"a = a - 2"</span>
&gt;&gt;&gt; <span class="keyword">print</span> a
2

&gt;&gt;&gt; a *= 5       <span class="comment"># same as</span> <span class="string">"a = a * 5"</span>
&gt;&gt;&gt; <span class="keyword">print</span> a
10

&gt;&gt;&gt; a /= 2       <span class="comment"># same as</span> <span class="string">"a = a/2"</span>
&gt;&gt;&gt; <span class="keyword">print</span> a
5
</pre>
<!--==============================-->
<hr width="50%">
<h3 class="try">Your turn</h3>
<p>Remember how difficult it was for Reeborg to add two numbers in base 10? Just to refresh your memory, we would like for Reeborg to be able to add two numbers in the following way:</p>
<p>
3+2=5 <img alt="3+2" src="../../images/intro/add3_2_start.png"><img alt="lead to"
src="../../images/lead_to.png"> <img alt="5" src=
"../../images/intro/add3_2_end.png">
</p>
<p>
8+4=12 <img alt="8+4" src="../../images/intro/add8_4_start.png"><img alt=
"lead to" src="../../images/lead_to.png"> <img alt="12" src=
"../../images/intro/add8_4_end.png"></p>
<p> You can use variables to make the task easier; when you do so, you don't need to have any special world, like we had to before, nor have Reeborg carry any beepers to start with.  Here are a few suggestions:</p>
<ul>
<li>Use a variable named <tt>sum</tt> whose value is originally set to 0.</li>
<li>Have Reeborg pick the beepers one by one, each time incrementing the value of <tt>sum</tt> by 1, perhaps using the notation <br />
<tt>sum += 1</tt>.</li>
<li>When Reeborg is done, consider two cases: if <tt>sum</tt> is greater than 10 or not.  If it is, have Reeborg drop all the beepers minus 10, move sideways and drop one (the carry over).  It if is not greater than 10, have Reeborg drop all the beepers.</li>
<li><span class="warning">Strong suggestion:</span> Do not define any function other than <tt>turn_right()</tt>.  You will see why in a few lessons from now.</li>
</ul>

<center><a href="26-interpreter2.htm"><img alt="previous" src=
"../../images/previous.png">Interpreting the Python keywords</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"28-variables2.htm">Variables and functions.<img alt="next"
src="../../images/next.png"></a></center>
</body>
</html>
